package com.lxp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.common.R;
import com.lxp.dto.DishDto;
import com.lxp.entity.Category;
import com.lxp.entity.Dish;
import com.lxp.service.CategoryService;
import com.lxp.service.DishFlavorService;
import com.lxp.service.DishService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lxp
 * @Motto 每天一行小代码，健康幸福学JAVA：
 * @create 2022-08-24 20:19
 * @Description:  菜品管理 控制类
 */


@RestController
@RequestMapping("/dish")
public class DishController {


    // 菜品
    @Autowired
    private DishService dishService;
    // 菜品口味
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;


    /**
     * 增加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        System.out.println(dishDto);
        dishService.saveWithFlavor(dishDto);
        return R.success("增加菜品成功");
    }

    /**
     * 分页查询菜品数据
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<IPage> page(Integer page,Integer pageSize,String name){
        // 创建分页查询构造器
        IPage<Dish> iPage = new Page<>(page,pageSize);
        IPage<DishDto> dishDtoIPage = new Page<>();

        // 创建条件查询构造器
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        // 添加过滤条件
        lqw.like(Strings.isNotEmpty(name),Dish::getName,name);

        // 添加排序条件
        lqw.orderByDesc(Dish::getUpdateTime);

        // 执行分页查询
        dishService.page(iPage,lqw);

        // 对象的拷贝
        BeanUtils.copyProperties(iPage,dishDtoIPage,"records");

        List<Dish> records = iPage.getRecords();

        List<DishDto> list = records.stream().map((item)->{
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item,dishDto);
            // 分类的Id
            Long categoryId = item.getCategoryId();

            // 根据id查询分类对象
            Category category = categoryService.getById(categoryId);
            System.out.println(category);
            // 获取分类的类型
            if (category != null ){
                dishDto.setCategoryName(category.getName());
            }

            return dishDto;
        }).collect(Collectors.toList());

        // 将查询到的类型数据添加到 dishDtoIPage
        dishDtoIPage.setRecords(list);


        return R.success(dishDtoIPage);

    }

    /**
     * 根据Id查询菜品信息和对应的口味信息
     * @param ids
     * @return
     */
    @GetMapping("/{ids}")
    public R<DishDto> get(@PathVariable Long ids){

        // 根据前端请求的 ID 查询
        DishDto dishDto = dishService.getByIdWithFlavor(ids);

        return R.success(dishDto);

    }

    /**
     * 修改菜品数据
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);

        return R.success("修改成功");
    }

    /**
     * 根据条件来查询对应的菜品数据
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<Dish>> list (Dish dish){
        // 构造查询条件
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        // 查询状态为 1 （起售状态）的菜品
        lqw.eq(Dish::getStatus,1);

        // 添加排序条件
        lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(lqw);


        return R.success(list);
    }

}
